import 'dart:async';
import 'package:flutter/services.dart';

class WifiPlugin {
  static const MethodChannel _channel = MethodChannel('wifi_plugin');

  static Future<List<WifiNetwork>> scanNetworks() async {
    final List<dynamic>? result = await _channel.invokeMethod('scanNetworks');
    if (result == null) return [];
    return result.map((e) {
      final m = Map<String, dynamic>.from(e);
      return WifiNetwork(
        ssid: m['ssid'] as String,
        isOpen: m['isOpen'] as bool,
        rssi: (m['rssi'] as num?)?.toInt() ?? -999,
        bssid: m['bssid'] as String,
      );
    }).toList();
  }

  /// 连接：密码可为空（开放网络）。返回是否成功。
  static Future<bool> connect({required String ssid, String? password}) async {
    final ok = await _channel.invokeMethod<bool>('connectWithPassword', {
      'ssid': ssid,
      'password': password ?? '',
    });
    return ok ?? false;
  }
}

class WifiNetwork {
  final String ssid;
  final bool isOpen;
  final int rssi;
  final String bssid;
  WifiNetwork({
    required this.ssid,
    required this.isOpen,
    required this.rssi,
    required this.bssid,
  });
}
